package netty;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import reflection.User;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: dch
 * @Date: 2020/6/5 17:40
 * @Description:
 */
public class nettyTest {
    public static void main(String[] args) {

        List<User> list=new ArrayList<User>();

        User student=new User(1,"bob");
        User student12=new User(2,"lily");

        list.add(student);
        list.add(student12);
        System.out.println("*******JSON.toJSONString(student)*******");
        String str1= JSON.toJSONString(student);
        System.out.println(str1);
        System.out.println(JSON.toJSONString(list));
        System.out.println();


        System.out.println("******JSON.parseObject(str1,Student.class)*******");
        //Student stu1=JSON.parseObject(str1,new TypeReference<Student>(){});
        User stu1=JSON.parseObject(str1,User.class);
        System.out.println(stu1);
        System.out.println();

        System.out.println("******j(JSONObject)JSON.toJSON(student)t******");
        JSONObject jsonObject1=(JSONObject)JSON.toJSON(student);
        System.out.println(jsonObject1.getString("name"));
        System.out.println();

        System.out.println("******JSON.toJavaObject(jsonObject1, Student.class)*****");
        User student2=JSON.toJavaObject(jsonObject1, User.class);
        System.out.println(student2);
        System.out.println();

        System.out.println("*******(JSONArray)JSON.toJSON(stulist)******");
        List<User> stulist=new ArrayList<User>();
        for(int i=0;i<5;i++){
            stulist.add(new User(i+2,"student"+i));

        }
        JSONArray jsonArrays=(JSONArray)JSON.toJSON(stulist);
        for(int i=0;i<jsonArrays.size();i++){
            System.out.println(jsonArrays.getJSONObject(i));
        }
        System.out.println();

        System.out.println("*****jjsonArrays.getJSONObject(i), Student.class*****");
        List<User> myList=new ArrayList<User>();
        for(int i=0;i<jsonArrays.size();i++){

            User student3=JSON.toJavaObject(jsonArrays.getJSONObject(i), User.class);
            myList.add(student3);
        }
        for(User stu:myList){
            System.out.println(stu);
        }

        System.out.println();

        System.out.println("*****JSON.toJSONString(jsonObject1);*****");
        String str4=JSON.toJSONString(jsonObject1);
        System.out.println(str4);
        System.out.println();

        System.out.println("*******JSON.parseObject(str1);*****");
        JSONObject jso1=JSON.parseObject(str1);
        System.out.println(jso1.getString("name"));
        System.out.println();

        System.out.println("*****JSON.parseArray(JSON.toJSONString(stulist)*****");
        JSONArray jArray=JSON.parseArray(JSON.toJSONString(stulist));
        for(int i=0;i<jArray.size();i++){
            System.out.println(jArray.getJSONObject(i));
        }
        System.out.println();
    }

}
